package flashSecurityServer.log;

import java.util.Properties;

import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import flashSecurityServer.log.LoggerConstants.CuteLoggerType;


public class EbocyLogManager{
	private static Properties properties=new Properties();

	private static String logPath="log/";
	public static  Logger getLogger(LoggerConstants loggerType,Level level
			,boolean isCreate){
		Logger logger=LogManager.exists(loggerType.getValue());
		if(logger==null){
			initProperties(loggerType.getValue(), level);
			PropertyConfigurator.configure(properties);
			if(isCreate){
				logger=Logger.getLogger(loggerType.getValue());
			}else{
				logger=Logger.getLogger(loggerType.getValue());
			}
		}
		return logger;
	}
	public static Logger getLogger(LoggerConstants loggerType, Level level) {
		return getLogger(loggerType, level, true);
	}

	public static Logger getRootLogger(){
		return getLogger(CuteLoggerType.ROOT, Level.ERROR);
	}
	/**
	 * @return
	 */
	public static Logger getSystemLogger(){
		return getLogger(CuteLoggerType.SYSTEM, Level.INFO);
	}
	private static void initProperties(String name, Level level){
		properties.setProperty("log4j.category."+name,level.toString()+","+name);
		properties.setProperty("log4j.additivity."+name, "true");
		properties.setProperty("log4j.appender."+name,
				"org.apache.log4j.DailyRollingFileAppender");
		properties.setProperty("log4j.appender."+name+".DatePattern", "'.'yyyy-MM-dd");
		properties.setProperty("log4j.appender."+name+".File", logPath+name);
		properties.setProperty("log4j.appender."+name+".layout",
				"org.apache.log4j.PatternLayout");
		properties.setProperty("log4j.appender."+name+".layout.ConversionPattern",
				"%d{yyyy-MM-dd HH:mm:ss}:%m%n");
	}

}
